<template>
  <a-card :bordered="false">

    <!-- 查询区域-start -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="refreshTable">
        <a-row :gutter="24">

          <a-col :md="6" :sm="12">
            <a-form-item label="账号">
              <a-input placeholder="输入账号模糊查询" v-model="queryParam.loginName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="refreshTable" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-end -->

    <!-- 操作按钮区域-start -->
    <div class="table-operator" style="border-top: 5px">
      <a-button type="primary" icon="plus" @click="handleAdd">添加用户</a-button>
    </div>
    <!-- 操作按钮区域-end -->

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <s-table
        ref="table"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      >

        <span slot="gender" slot-scope="text">
          <span v-if="text == userGenderEnum.MALE.KEY">{{ userGenderEnum.MALE.NAME }}</span>
          <span v-else-if="text == userGenderEnum.FEMALE.KEY">{{ userGenderEnum.FEMALE.NAME }}</span>
          <span v-else-if="text == userGenderEnum.NONE.KEY">{{ userGenderEnum.NONE.NAME }}</span>
          <span v-else>{{ '未知' }}</span>
        </span>

        <span slot="status" slot-scope="text">
          <span v-if="text == userStatusEnum.ENABLE.KEY">{{ userStatusEnum.ENABLE.NAME }}</span>
          <span v-else-if="text == userStatusEnum.DISABLE.KEY">{{ userStatusEnum.DISABLE.NAME }}</span>
          <span v-else>{{ '未知' }}</span>
        </span>

        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定重置密码吗?" @confirm="() => resetPwd(record.id)">
            <a>重置密码</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => del(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
    </div>
    <!-- table区域-end -->

    <user-modal ref="modalForm" @ok="refreshTable"></user-modal>

  </a-card>
</template>

<script>
import UserModal from './modules/UserModal'
import { ListMixin } from '@/mixins/ListMixin'
import { apiListUser, apiDelUser, apiResetPwd } from '@/api/system'
import { USER_GENDER_ENUM, USER_STATUS_ENUM } from '@/store/mutation-types'

const columns = [
  {
    title: '登录账号',
    align: 'center',
    dataIndex: 'loginName'
  },
  {
    title: '用户姓名',
    align: 'center',
    dataIndex: 'userName'
  },
  {
    title: '员工编号',
    align: 'center',
    dataIndex: 'employeeCode'
  },
  {
    title: '性别',
    align: 'center',
    dataIndex: 'gender',
    scopedSlots: { customRender: 'gender' }
  },
  {
    title: '手机号码',
    align: 'center',
    dataIndex: 'mobile'
  },
  {
    title: '状态',
    align: 'center',
    width: 80,
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    scopedSlots: { customRender: 'action' },
    align: 'center',
    width: 200
  }
]
export default {
  name: 'UserList',
  mixins: [ListMixin],
  components: {
    UserModal
  },
  data () {
    return {
      columns,
      loadData: parameter => {
        return apiListUser(Object.assign(parameter, this.queryParam)).then(res => {
          return res
        })
      },
      spinningModal: false,
      userGenderEnum: USER_GENDER_ENUM,
      userStatusEnum: USER_STATUS_ENUM
    }
  },
  methods: {
    handleAdd () {
      this.$refs.modalForm.show('新增')
    },
    handleEdit (record) {
      this.$refs.modalForm.show('编辑', record)
    },
    resetPwd (id) {
      apiResetPwd(id).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg)
          this.refreshTable()
        } else {
          this.$message.warning(res.msg)
        }
      })
    },
    del (id) {
      apiDelUser(id).then(res => {
        if (res.code === 1) {
          this.$message.success(res.msg)
          this.refreshTable()
        } else {
          this.$message.warning(res.msg)
        }
      })
    }
  }
}
</script>
